export default {
  data() {
    return {
      tableData: [],
      page: 1,
      limit: 10,
      count: 0,
      type: 1,
      operatorList: [], //供应商列表
      operator_id: "",
      key_words: '',
      dialogVisible: false, // 是否退款 弹窗
      refund_order_id: '', //退款订单号
      refund_order_price: '', //退款金额
      // 退款金额
      ruleForm: {
        money: '',
      },
      // 验证
      rules: {
        money: [{
          required: true,
          message: '请正确输入退款金额',
          trigger: 'blur'
        }, ],
      },
      select_value: '', //选择框内容
      select_time: '', //日期选择器
    }
  },
  //进入页面加载
  mounted: function () {
    var that = this;
    //在缓存中获取值
    that.user_name = sessionStorage.getItem("user_name");
    that.user_id = sessionStorage.getItem("user_id");
    that.token = sessionStorage.getItem("access-token");
    if ((!that.token) || (!that.user_id) || (!that.user_name)) {
      that.$router.push('/login');
    }
    that.getList();
  },

  //方法
  methods: {
    /**
     * 选择类型
     * @param tab
     */
    handleClick(tab) {
      let that = this;
      that.type = tab;
      that.getList();
    },

    /**
     * 筛选
     */
    search: function () {
      this.getList();
    },

    /**
     * 列表
     */
    getList() {
      var that = this;
      that.tableData = [];
      //请求的数据
      var formData = {};
      formData.token = that.token;
      formData.type = that.type;
      formData.seller_id = that.user_id;

      //时间条件
      if (that.select_time) {
        formData.create_time = that.formatDateTime(that.select_time[0]);
        formData.last_time = that.formatDateTime(that.select_time[1]);
      }

      if (that.key_words) {
        formData.keys = that.key_words;
      }

      //请求邀请者列表
      that.$http.post(that.adminApi.api_url + "/Operatorder/orderlist", formData, {
        emulateJSON: true
      }).then(function (res) {
          // 处理成功的结果
          if (res.body) {
            var data = res.body.list;

            if (data) {
              for (let i = 0; i < data.length; i++) {

                // 判断订单状态
                out: if (data[i].is_pay == 1) {
                  data[i].oerder_status = '已支付';

                  if (data[i].is_refund == 1) {
                    data[i].oerder_status = '已退款';
                    break out;
                  }

                  if (data[i].is_travel == 1) {
                    data[i].oerder_status = '已出行';

                    if (data[i].is_over == 1) {
                      data[i].oerder_status = '已完成';
                      break out;
                    } else {
                      data[i].oerder_status = '未完成';
                      break out;
                    }
                  } else {
                    data[i].oerder_status = '未出行';
                    break out;
                  }

                } else {
                  data[i].oerder_status = '待支付';
                }
                
              }
              that.tableData = data;
            }


            if (res.body.count) {
              that.count = res.body.count;
            } else {
              that.count = 0;
            }
          }
        },
        function () {
          // 处理失败的结果
          that.$message({
            type: 'error',
            message: `操作提示: ${ '处理异常' }`
          });
        });
    },

    /**
     * 下一页
     * @param currentPage
     */
    handleCurrentChange: function (currentPage) {
      var that = this;
      that.page = currentPage;
    },

    /**
     * 时间转换
     * @param date
     * @returns {string}
     */
    formatDateTime: function (date) {
      var y = date.getFullYear();
      var m = date.getMonth() + 1;
      m = m < 10 ? ('0' + m) : m;
      var d = date.getDate();
      d = d < 10 ? ('0' + d) : d;
      var h = date.getHours();
      var minute = date.getMinutes();
      minute = minute < 10 ? ('0' + minute) : minute;
      return y + '-' + m + '-' + d;
    },

    /**
     * 查看订单
     */
    see_order: function (order_id) {
      this.$router.push({
        path: '/operatorder/see_order',
        query: {
          order_id: order_id
        }
      })
    },

    /**
     * 取消订单
     */
    cancel_order(order_id) {
      var that = this;
      this.$confirm('是否 取消 该订单', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {

        // 取消订单
        that.$http.post(that.adminApi.api_url + "/Operatorder/cancelorder", {
          token: that.token,
          seller_id: that.user_id,
          order_id: order_id
        }, {
          emulateJSON: true
        }).then(function (res) {
          if (res.body.status == 1) {

            that.getList();

            that.$message({
              type: 'success',
              message: '取消成功!'
            });
          } else {
            that.$message({
              type: 'error',
              message: '取消失败!'
            });
          }
        });

      });
    },

    /**
     * 确认订单
     */
    confirm_order(order_id) {
      var that = this;

      this.$confirm('是否 确认 该订单', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {

        // 确认订单
        that.$http.post(that.adminApi.api_url + "/Operatorder/trueorder", {
          token: that.token,
          seller_id: that.user_id,
          order_id: order_id
        }, {
          emulateJSON: true
        }).then(function (res) {
          if (res.body.status == 1) {

            that.getList();
            that.$message({
              type: 'success',
              message: '确认成功!'
            });
          } else {
            that.$message({
              type: 'error',
              message: '确认失败!'
            });
          }
        });
      });
    },

    /**
     * 点击退款按钮
     */
    refund_btn(order_id, total_price) {
      // 退款订单号
      this.refund_order_id = order_id;
      // 退款订单金额
      this.refund_order_price = total_price;
      // 确认订单 弹窗
      this.dialogVisible = true;
    },

    /**
     * 确认退款按钮
     */
    confirm_refund() {
      var money = this.ruleForm.money.trim() - 0;
      var order_price = this.refund_order_price;

      if (money > order_price || !Number.isInteger(money)) {
        this.$message.warning('请正确输入金额');

        return;
      }

      var that = this;
      that.$http.post(that.adminApi.api_url + "/Operatorder/wx_refund", {
        token: that.token,
        return_price: that.ruleForm.money,
        order_id: that.refund_order_id //退款订单号
      }, {
        emulateJSON: true
      }).then(function (res) {
        var data = res.body;
        if (data.status == 1) that.$message.success('退款成功');

        that.getList();
      })
    },
  }
}